#include <iostream>
#include "log.hpp"
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <unistd.h>
#include <memory.h>
#include <memory>
#include "Thread.hpp"

uint16_t serverport = 0;
std::string serverip;

static void usage(std::string proc)
{
    std::cout << "\nUsage:" << proc << " :ip  port" << std::endl;
}

static void *udp_send(void *arg)
{
    int sock = *(int *)(((ThreadData *)arg)->_args);
    std::string name = ((ThreadData *)arg)->_name;

    std::string messages;
    struct sockaddr_in server;
    memset(&server, 0, sizeof server);
    server.sin_family = AF_INET;
    server.sin_port = htons(serverport);
    server.sin_addr.s_addr = inet_addr(serverip.c_str());
    while (1)
    {
        std::cout << "请输入消息：";
        std::getline(std::cin, messages);
        if (messages == "quit")
            break;
        // 发送信息，要把自己的server信息发过去
        sendto(sock, messages.c_str(), messages.size(), 0, (struct sockaddr *)&server, sizeof server);
    }
}

static void *udp_rev(void *arg)
{
    int sock = *(int *)(((ThreadData *)arg)->_args);
    std::string name = ((ThreadData *)arg)->_name;

    char buffer[1024];
    // 接收消息，需要一个空的rev来接收
    while (1)
    {
        struct sockaddr_in rev;
        socklen_t len = sizeof rev;
        int s = recvfrom(sock, buffer, sizeof buffer, 0, (struct sockaddr *)&rev, &len);
        if (s > 0)
        {
            buffer[s] = 0;
            std::cout << "echo->" << buffer << std::endl;
        }
        else if (s == 0)
        {
            logMessage(NORMAL, "can`t read message");
        }
        else
        {
            logMessage(ERROR, "read message failed");
            break;
        }
    }
}

int main(int argc, char *argv[])
{
    if (argc != 3)
    {
        usage(argv[0]);
        exit(1);
    }
    // 获得套接字
    int sock = socket(AF_INET, SOCK_DGRAM, 0);
    if (sock < 0)
    {
        std::cerr << "socket error" << std::endl;
        exit(2);
    }
    // 多线程
    // 存储传进来的ip和port
    serverip = argv[1];
    serverport = atoi(argv[2]);
    // 多线程的创建
    // 使用智能指针，一个线程接收消息，一个线程发送消息
    std::unique_ptr<Thread> sendT(new Thread(1, udp_send, (void *)&sock));
    std::unique_ptr<Thread> revT(new Thread(2, udp_rev, (void *)&sock));

    sendT->start();
    revT->start();

    sendT->join();
    revT->join();

    close(sock);
    // 单线程
    // 客户端不需要绑定端口
    // client一般不需要显示的bind指定port，而是让OS自动随机选择(什么时候做的呢？)
    //  struct sockaddr_in server;
    //  memset(&server,0,sizeof server);
    //  server.sin_family = AF_INET;
    //  server.sin_addr.s_addr = inet_addr(argv[1]);
    //  server.sin_port = htons(atoi(argv[2]));
    //  char buffer[1024];
    //  std::string message;
    //  while(1)
    //  {
    //      std::cout << "请输入：" ;
    //      std::getline(std::cin,message);
    //      if(message == "quit" ) break;
    //      //当client首次发送消息给服务器的时候，OS会自动给client bind他的IP和PORT
    //      sendto(sock,message.c_str(),message.size(),0,(struct sockaddr*)&server,sizeof server);

    //     struct sockaddr_in temp;
    //     socklen_t len = sizeof(temp);
    //     ssize_t s = recvfrom(sock,buffer,sizeof(buffer),0,(struct sockaddr*)&temp,&len);
    //     if(s > 0)
    //     {
    //         buffer[s] = 0;
    //         std::cout << "server echo# " << buffer << std::endl;
    //     }
    // }
    // close(sock);
    return 0;
}